<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="微信id" prop="wid">
        <el-input
          v-model="queryParams.wid"
          placeholder="请输入微信id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份标识" prop="appid">
        <el-input
          v-model="queryParams.appid"
          placeholder="请输入身份标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份密钥" prop="secret">
        <el-input
          v-model="queryParams.secret"
          placeholder="请输入身份密钥"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配置token" prop="token">
        <el-input
          v-model="queryParams.token"
          placeholder="请输入配置token"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="aeskey" prop="aeskey">
        <el-input
          v-model="queryParams.aeskey"
          placeholder="请输入aeskey"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否支付" prop="isPay">
        <el-input
          v-model="queryParams.isPay"
          placeholder="请输入是否支付"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户号" prop="partner">
        <el-input
          v-model="queryParams.partner"
          placeholder="请输入商户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付密钥" prop="partnerKey">
        <el-input
          v-model="queryParams.partnerKey"
          placeholder="请输入支付密钥"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否使用" prop="isused">
        <el-input
          v-model="queryParams.isused"
          placeholder="请输入是否使用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回调地址" prop="redirectUrl">
        <el-input
          v-model="queryParams.redirectUrl"
          placeholder="请输入回调地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="apiv3key" prop="apivkey">
        <el-input
          v-model="queryParams.apivkey"
          placeholder="请输入apiv3key"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本地keypath" prop="ikeyPath">
        <el-input
          v-model="queryParams.ikeyPath"
          placeholder="请输入本地keypath"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="keypath" prop="keyPath">
        <el-input
          v-model="queryParams.keyPath"
          placeholder="请输入keypath"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本地PrivateKeypath" prop="iprivateKeyPath">
        <el-input
          v-model="queryParams.iprivateKeyPath"
          placeholder="请输入本地PrivateKeypath"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="PrivateKeypath" prop="privateKeyPath">
        <el-input
          v-model="queryParams.privateKeyPath"
          placeholder="请输入PrivateKeypath"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本地PrivateCertpath" prop="iprivateCertPath">
        <el-input
          v-model="queryParams.iprivateCertPath"
          placeholder="请输入本地PrivateCertpath"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="PrivateCertpath" prop="privateCertPath">
        <el-input
          v-model="queryParams.privateCertPath"
          placeholder="请输入PrivateCertpath"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="CertSerialNo" prop="certSerialNo">
        <el-input
          v-model="queryParams.certSerialNo"
          placeholder="请输入CertSerialNo"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['core:config:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['core:config:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['core:config:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['core:config:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="true"/>
      <el-table-column label="微信id" align="center" prop="wid" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="身份标识" align="center" prop="appid" />
      <el-table-column label="身份密钥" align="center" prop="secret" />
      <el-table-column label="配置token" align="center" prop="token" />
      <el-table-column label="aeskey" align="center" prop="aeskey" />
      <el-table-column label="是否支付" align="center" prop="isPay" />
      <el-table-column label="商户号" align="center" prop="partner" />
      <el-table-column label="支付密钥" align="center" prop="partnerKey" />
      <el-table-column label="是否使用" align="center" prop="isused" />
      <el-table-column label="回调地址" align="center" prop="redirectUrl" />
      <el-table-column label="apiv3key" align="center" prop="apivkey" />
      <el-table-column label="本地keypath" align="center" prop="ikeyPath" />
      <el-table-column label="keypath" align="center" prop="keyPath" />
      <el-table-column label="本地PrivateKeypath" align="center" prop="iprivateKeyPath" />
      <el-table-column label="PrivateKeypath" align="center" prop="privateKeyPath" />
      <el-table-column label="本地PrivateCertpath" align="center" prop="iprivateCertPath" />
      <el-table-column label="PrivateCertpath" align="center" prop="privateCertPath" />
      <el-table-column label="CertSerialNo" align="center" prop="certSerialNo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['core:config:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['core:config:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改身份标识对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="微信id" prop="wid">
          <el-input v-model="form.wid" placeholder="请输入微信id" />
        </el-form-item>
        <el-form-item label="身份标识" prop="appid">
          <el-input v-model="form.appid" placeholder="请输入身份标识" />
        </el-form-item>
        <el-form-item label="身份密钥" prop="secret">
          <el-input v-model="form.secret" placeholder="请输入身份密钥" />
        </el-form-item>
        <el-form-item label="配置token" prop="token">
          <el-input v-model="form.token" placeholder="请输入配置token" />
        </el-form-item>
        <el-form-item label="aeskey" prop="aeskey">
          <el-input v-model="form.aeskey" placeholder="请输入aeskey" />
        </el-form-item>
        <el-form-item label="是否支付" prop="isPay">
          <el-input v-model="form.isPay" placeholder="请输入是否支付" />
        </el-form-item>
        <el-form-item label="商户号" prop="partner">
          <el-input v-model="form.partner" placeholder="请输入商户号" />
        </el-form-item>
        <el-form-item label="支付密钥" prop="partnerKey">
          <el-input v-model="form.partnerKey" placeholder="请输入支付密钥" />
        </el-form-item>
        <el-form-item label="是否使用" prop="isused">
          <el-input v-model="form.isused" placeholder="请输入是否使用" />
        </el-form-item>
        <el-form-item label="回调地址" prop="redirectUrl">
          <el-input v-model="form.redirectUrl" placeholder="请输入回调地址" />
        </el-form-item>
        <el-form-item label="apiv3key" prop="apivkey">
          <el-input v-model="form.apivkey" placeholder="请输入apiv3key" />
        </el-form-item>
        <el-form-item label="本地keypath" prop="ikeyPath">
          <el-input v-model="form.ikeyPath" placeholder="请输入本地keypath" />
        </el-form-item>
        <el-form-item label="keypath" prop="keyPath">
          <el-input v-model="form.keyPath" placeholder="请输入keypath" />
        </el-form-item>
        <el-form-item label="本地PrivateKeypath" prop="iprivateKeyPath">
          <el-input v-model="form.iprivateKeyPath" placeholder="请输入本地PrivateKeypath" />
        </el-form-item>
        <el-form-item label="PrivateKeypath" prop="privateKeyPath">
          <el-input v-model="form.privateKeyPath" placeholder="请输入PrivateKeypath" />
        </el-form-item>
        <el-form-item label="本地PrivateCertpath" prop="iprivateCertPath">
          <el-input v-model="form.iprivateCertPath" placeholder="请输入本地PrivateCertpath" />
        </el-form-item>
        <el-form-item label="PrivateCertpath" prop="privateCertPath">
          <el-input v-model="form.privateCertPath" placeholder="请输入PrivateCertpath" />
        </el-form-item>
        <el-form-item label="CertSerialNo" prop="certSerialNo">
          <el-input v-model="form.certSerialNo" placeholder="请输入CertSerialNo" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig } from "@/api/core/config";

export default {
  name: "Config",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 身份标识表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wid: undefined,
        type: undefined,
        appid: undefined,
        secret: undefined,
        token: undefined,
        aeskey: undefined,
        isPay: undefined,
        partner: undefined,
        partnerKey: undefined,
        isused: undefined,
        redirectUrl: undefined,
        apivkey: undefined,
        ikeyPath: undefined,
        keyPath: undefined,
        iprivateKeyPath: undefined,
        privateKeyPath: undefined,
        iprivateCertPath: undefined,
        privateCertPath: undefined,
        certSerialNo: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询身份标识列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(response => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        wid: undefined,
        type: undefined,
        appid: undefined,
        secret: undefined,
        token: undefined,
        aeskey: undefined,
        isPay: undefined,
        partner: undefined,
        partnerKey: undefined,
        isused: undefined,
        redirectUrl: undefined,
        apivkey: undefined,
        ikeyPath: undefined,
        keyPath: undefined,
        iprivateKeyPath: undefined,
        privateKeyPath: undefined,
        iprivateCertPath: undefined,
        privateCertPath: undefined,
        certSerialNo: undefined,
        delFlag: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加身份标识";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getConfig(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改身份标识";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除身份标识编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delConfig(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('core/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
